package com.music.order.controller.web;

import com.music.common.core.enums.SysType;
import com.music.common.core.utils.token.AccToken;
import com.music.common.core.utils.token.TokenUtils;
import com.music.common.core.web.domain.AjaxResult;
import com.music.order.domain.Order;
import com.music.order.dto.OrderDto;
import com.music.order.service.OrderService;
import com.music.order.vo.OrderVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/order/web")
public class OrderWebController {
    @Resource
    private OrderService orderServiceImpl;

    @GetMapping("/find")
    public AjaxResult find(HttpServletRequest request){
        System.out.println();
        AccToken token = TokenUtils.getToken(request, SysType.PORTAL);
        Long userId = token.getUserId();
        Order order = new Order();
        order.setUserId(userId);
        order.setOrderDelete(0);
        List<OrderVo> orderList = orderServiceImpl.selectOrderVoList(order);
        return AjaxResult.success(orderList);
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody Order order){
        orderServiceImpl.deleteByOrderId(order.getOrderId());
        return AjaxResult.success("删除成功");
    }

    @RequestMapping("/getOrderSn")
    public AjaxResult getOrderSn(){
        String snowId = orderServiceImpl.snowFlakeIdGenerate();
        System.out.println(snowId);
        return AjaxResult.success(snowId);
    }

//    @GlobalTransactional
    @RequestMapping("/saleMusic")
    public AjaxResult saleMusic(@RequestBody OrderDto orderDto, HttpServletRequest request){
        Long activateUserId = orderServiceImpl.activate(orderDto.getActivateUserAccount());
        orderDto.setActivateUserId(activateUserId);
        System.out.println(orderDto);
        //1.校验订单号,防止被篡改
        Boolean flag = orderServiceImpl.isReal(orderDto.getOrderId());

        //2.创建订单
        AccToken token = TokenUtils.getToken(request, SysType.PORTAL);
        Long userId = token.getUserId();
        orderDto.setUserId(userId);

        orderServiceImpl.saleMusic(orderDto);
        return AjaxResult.success("ok");
    }
}
